use crate::kline_chart;
use crate::navigation::Navigation;
use crate::table_symbol::TableSymbol;
use crate::tool_menu::ToolMenu;
use crate::top_menu::TopMenu;

use eframe::egui::{self, ColorImage};
use std::sync::Arc;

pub struct MyApp {
    top_menu: TopMenu,
    tool_menu: ToolMenu,
    table_symbol: TableSymbol,
    navigation: Navigation,
}

impl MyApp {
    pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
        setup_custom_fonts(&cc.egui_ctx);
        Self {
            top_menu: TopMenu::new(),
            tool_menu: ToolMenu::new(),
            table_symbol: TableSymbol::default(),
            navigation: Navigation::new(),
        }
    }
}

impl eframe::App for MyApp {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
            self.top_menu.show(ctx, ui);
            ui.add_space(5.0);
            ui.separator();
            ui.add_space(5.0);
            self.tool_menu.show(ui);
            ui.add_space(10.0);
        });

        egui::SidePanel::left("left_panel")
            .resizable(true)
            .default_width(260.0)
            .show(ctx, |ui| {
                ui.vertical(|ui| {
                    // ui.heading("Left Panel");
                    self.table_symbol.show(ui);
                    ui.separator();
                    self.navigation.show(ui);
                });
            });
        egui::CentralPanel::default().show(ctx, |ui| {
            // Main content area
            // ui.label("Main Content Here");
            kline_chart::draw_kline_chart(ui);
        });
    }
}

fn setup_custom_fonts(ctx: &egui::Context) {
    // Start with the default fonts (we will be adding to them rather than replacing them).
    let mut fonts = egui::FontDefinitions::default();

    // Install my own font (maybe supporting non-latin characters).
    // .ttf and .otf files supported.
    fonts.font_data.insert(
        "my_font".to_owned(),
        egui::FontData::from_static(include_bytes!("../assets/AlibabaPuHuiTi.ttf")),
    );

    // Put my font first (highest priority) for proportional text:
    fonts
        .families
        .entry(egui::FontFamily::Proportional)
        .or_default()
        .insert(0, "my_font".to_owned());

    // Put my font as last fallback for monospace:
    fonts
        .families
        .entry(egui::FontFamily::Monospace)
        .or_default()
        .push("my_font".to_owned());

    // Tell egui to use these fonts:
    ctx.set_fonts(fonts);
}
